﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _1b
{
    public partial class Form1 : Form
    {
        private Random random;
        private int timeLeft;
        private int score;
        private int totalQuestions;
        public Form1()
        {
            InitializeComponent();
            random = new Random();
            timeLeft = 30; // 设置时间限制为30秒
            score = 0;
            totalQuestions = 0;
        }

        private string GenerateQuestion()
        {
            int num1 = random.Next(1, 101); // 随机生成1-100之间的数
            int num2 = random.Next(1, 101);
            int operatorIndex = random.Next(0, 2); // 随机生成0或1，代表“+”或“-”

            string question = "";

            switch (operatorIndex)
            {
                case 0:
                    question = $"{num1} + {num2} =";
                    break;
                case 1:
                    question = $"{num1} - {num2} =";
                    break;
            }

            return question;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
                int answer;
                bool isNumeric = int.TryParse(txtAnswer.Text, out answer);

                if (isNumeric)
                {
                    int operatorIndex = lblQuestion.Text.Contains("+") ? 0 : 1;
                    int num1 = int.Parse(lblQuestion.Text.Split(' ')[0]);
                    int num2 = int.Parse(lblQuestion.Text.Split(' ')[2]);
                    int correctAnswer = operatorIndex == 0 ? num1 + num2 : num1 - num2;

                    if (answer == correctAnswer)
                    {
                        score++;
                        lblScore.Text = $"得分：{score}";
                        MessageBox.Show("回答正确！");
                    }
                    else
                    {
                        MessageBox.Show("回答错误！");
                    }

                    totalQuestions++;
                    lblQuestion.Text = GenerateQuestion();
                    txtAnswer.Text = "";
                    txtAnswer.Focus();
                }
                else
                {
                    MessageBox.Show("请输入一个有效的整数！");
                }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lblTimeLeft.Text = $"剩余时间：{timeLeft}秒";
            lblScore.Text = "得分：0";
            lblQuestion.Text = GenerateQuestion();
            txtAnswer.Focus();

            // 启动定时器
            timer1.Start();
        }

        private void lblTimeLeft_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (timeLeft > 0)
            {
                timeLeft--;
                lblTimeLeft.Text = $"剩余时间：{timeLeft}秒";
            }
            else
            {
                timer1.Stop();
                MessageBox.Show("时间已用完！");
                ShowScore();
            }
        }
        private void ShowScore()
        {
            double percentage = (double)score / totalQuestions * 100;
            MessageBox.Show($"答题结果：\n\n总题数：{totalQuestions}\n得分：{score}\n正确率：{percentage}%");
        }
    }
}
